package com.ep.thirdPlatformProject.job.jobhandler;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.StrUtil;
import com.ep.thirdPlatformProject.job.service.ErpToOAService;
import com.ep.thirdPlatformProject.system.util.SimpleDealUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class ErpToOAJob {

    private final ErpToOAService erpToOAService;

    /**
     * 生成入职单
     */
    @XxlJob("syncBudgetForm")
    public void syncBudgetForm() {
        String jobName= "ERP->OA同步生成预算审批单";
        try {
            StopWatch watch = DateUtil.createStopWatch();
            watch.start("任务开始");

            String jobParam = XxlJobHelper.getJobParam();
            String[] deptIds = SimpleDealUtil.paramValidateToDeptId(jobParam);
            String response = erpToOAService.syncBudgetForm(deptIds);


            watch.stop();
            XxlJobHelper.log("任务结束，处理任务耗时：{}ms", watch.getTotalTimeMillis());
            XxlJobHelper.handleSuccess("\n" + response);
        } catch (Exception e) {
            log.error("{}定时任务执行，{}", jobName, e.getMessage(), e);
            XxlJobHelper.handleFail(
                    StrUtil.format("{}定时任务执行失败，{}", jobName, e.getMessage())
            );
        }
    }
}
